<template>
	<view class="page">
		<!-- 头部 - 仅在APP环境显示 -->
		<!-- #ifdef APP-PLUS -->
		<view class="header">
			<view class="header-content">
				<view class="back-btn" @click="$jump.navigateBack()">
					<u-icon name="arrow-left" color="#333333" size="42"></u-icon>
				</view>
				<view class="title">系统消息</view>
				<view class="placeholder"></view>
			</view>
		</view>
		<!-- #endif -->
		
		<!-- 消息列表 -->
		<view class="message-list">
			<view v-if="messages.length === 0 && !isLoading" class="empty-state">
				<image src="https://www.shuyunpt.com/app/image/empty/empty-message.png" class="empty-icon"></image>
				<view class="empty-text">暂无系统消息</view>
				<view class="empty-desc">您还没有收到任何系统消息~</view>
			</view>
			
			<view v-else>
				<view 
					class="message-item" 
					v-for="(item, index) in messages" 
					:key="index"
					@click="viewMessageDetail(item)"
				>
					<view class="message-header">
						<text class="message-title">{{item.title}}</text>
						<text class="message-time">{{formatTime(item.created_at)}}</text>
					</view>
					<view class="message-content">
						<text class="message-detail">{{item.content}}</text>
					</view>
					<!-- 未读标记 -->
					<view v-if="item.is_read === 0" class="unread-dot"></view>
				</view>
			</view>
		</view>
		
		<!-- 加载更多 -->
		<view v-if="isLoading" class="loading-more">
			<text>加载中...</text>
		</view>
		<view v-else-if="!hasMore && messages.length > 0" class="loading-more">
			<text>没有更多消息了</text>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				messages: [],
				isLoading: false,
				page: 1,
				pageSize: 20,
				hasMore: true
			};
		},
		onLoad() {
			this.getSystemMessages();
		},
		onPullDownRefresh() {
			this.refreshMessages();
		},
		onReachBottom() {
			this.loadMoreMessages();
		},
		methods: {
			// 获取系统消息列表
			async getSystemMessages(isLoadMore = false) {
				if (this.isLoading) return;
				
				this.isLoading = true;
				
				try {
					const res = await this.$service({
						url: '/api/member.message/getSystemMessageList',
						data: {
							page: this.page,
							page_size: this.pageSize
						}
					});
					
					if (res.code === 1) {
						const newMessages = res.data || [];
						
						if (isLoadMore) {
							this.messages.push(...newMessages);
						} else {
							this.messages = newMessages;
						}
						
						this.hasMore = newMessages.length >= this.pageSize;
					} else {
						this.$hint.desc(res.msg || '获取系统消息失败');
					}
				} catch (error) {
					console.error('获取系统消息失败:', error);
					this.$hint.desc('获取系统消息失败');
				} finally {
					this.isLoading = false;
					uni.stopPullDownRefresh();
				}
			},
			
			// 刷新消息
			async refreshMessages() {
				this.page = 1;
				this.hasMore = true;
				await this.getSystemMessages();
			},
			
			// 加载更多消息
			async loadMoreMessages() {
				if (!this.hasMore || this.isLoading) return;
				
				this.page++;
				await this.getSystemMessages(true);
			},
			
			// 查看消息详情
			async viewMessageDetail(item) {
				// 标记消息为已读
				if (item.is_read === 0) {
					await this.markAsRead(item.id);
					item.is_read = 1;
				}
				
				// 显示消息详情
				uni.showModal({
					title: item.title,
					content: item.content,
					showCancel: false,
					confirmText: '确定'
				});
			},
			
			// 标记消息为已读
			async markAsRead(messageId) {
				try {
					await this.$service({
						url: '/api/member.message/markSystemMessageRead',
						data: {
							message_id: messageId
						}
					});
				} catch (error) {
					console.error('标记已读失败:', error);
				}
			},
			
			// 格式化时间
			formatTime(timeStr) {
				if (!timeStr) return '';
				
				const now = new Date();
				const time = new Date(timeStr);
				const diff = now - time;
				
				// 小于1分钟
				if (diff < 60000) {
					return '刚刚';
				}
				// 小于1小时
				if (diff < 3600000) {
					return Math.floor(diff / 60000) + '分钟前';
				}
				// 小于24小时
				if (diff < 86400000) {
					return Math.floor(diff / 3600000) + '小时前';
				}
				// 小于7天
				if (diff < 604800000) {
					return Math.floor(diff / 86400000) + '天前';
				}
				// 超过7天显示具体日期
				return `${time.getMonth() + 1}-${time.getDate()}`;
			}
		}
	}
</script>

<style lang="scss" scoped>
	.page {
		min-height: 100vh;
		background: #f5f5f5;
		padding-bottom: 40rpx;
		/* 兼容不同平台 */
		box-sizing: border-box;
	}
	
	/* 兼容不同平台的通用样式 */
	/* #ifdef APP-PLUS */
	/* APP平台特定样式 */
	.page {
		/* 防止内容被状态栏遮挡 */
		padding-top: var(--status-bar-height);
	}
	/* #endif */
	
	/* #ifdef MP-WEIXIN */
	/* 微信小程序特定样式 */
	.page {
		/* 微信小程序页面背景 */
		background: #f5f5f5;
		/* 微信小程序有导航栏标题，内容从导航栏下方开始 */
		box-sizing: border-box;
	}
	/* #endif */
	
	/* 头部样式 - 仅在APP环境显示 */
	/* #ifdef APP-PLUS */
	.header {
		background: #ffffff;
		padding: 20rpx 0;
		box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.1);
		margin-bottom: 20rpx;
		padding-top: calc(20rpx + var(--status-bar-height));
	}
	
	.header-content {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 0 40rpx;
	}
	
	.back-btn {
		padding: 10rpx;
		min-width: 60rpx;
		min-height: 60rpx;
		display: flex;
		align-items: center;
		justify-content: center;
	}
	
	.title {
		font-size: 36rpx;
		font-weight: bold;
		color: #333333;
		font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
	}
	
	.placeholder {
		width: 60rpx;
	}
	/* #endif */
	
	/* 消息列表样式 */
	.message-list {
		padding: 0 20rpx;
		/* 兼容不同平台的间距 */
		/* #ifdef APP-PLUS */
		margin-top: 20rpx;
		/* #endif */
		/* #ifdef MP-WEIXIN */
		margin-top: 40rpx;
		/* #endif */
	}
	
	.message-item {
		background: #ffffff;
		border-radius: 16rpx;
		padding: 30rpx;
		margin-bottom: 20rpx;
		box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
		transition: all 0.3s ease;
		position: relative;
	}
	
	.message-item:active {
		transform: scale(0.98);
		background: #f8f9fa;
	}
	
	.message-header {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-bottom: 15rpx;
	}
	
	.message-title {
		font-size: 32rpx;
		font-weight: bold;
		color: #333333;
		flex: 1;
		margin-right: 20rpx;
	}
	
	.message-time {
		font-size: 24rpx;
		color: #999999;
		white-space: nowrap;
	}
	
	.message-content {
		display: flex;
		align-items: flex-start;
	}
	
	.message-detail {
		font-size: 28rpx;
		color: #666666;
		line-height: 1.5;
		flex: 1;
		display: -webkit-box;
		-webkit-line-clamp: 3;
		-webkit-box-orient: vertical;
		overflow: hidden;
		text-overflow: ellipsis;
	}
	
	/* 未读标记 */
	.unread-dot {
		position: absolute;
		top: 20rpx;
		right: 20rpx;
		width: 12rpx;
		height: 12rpx;
		background: #ff4757;
		border-radius: 50%;
	}
	
	/* 空状态 */
	.empty-state {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		padding: 200rpx 0;
		text-align: center;
	}
	
	.empty-icon {
		width: 240rpx;
		height: 240rpx;
		margin-bottom: 40rpx;
		opacity: 0.6;
	}
	
	.empty-text {
		font-size: 32rpx;
		color: #333333;
		font-weight: 500;
		margin-bottom: 20rpx;
	}
	
	.empty-desc {
		font-size: 28rpx;
		color: #999999;
		line-height: 1.5;
	}
	
	/* 加载更多 */
	.loading-more {
		text-align: center;
		padding: 30rpx 0;
		color: #999999;
		font-size: 28rpx;
	}
</style> 